home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / (A)P / (A)P1.ADF / Polygon / iffwriter / iff.h next >
C/C++ Source or Header  |  1987-05-25  |  20KB  |  429 lines

  1. #ifndef IFF_H
  2. #define IFF_H
  3. /*----------------------------------------------------------------------*/
  4. /* IFF.H  defs for IFF-85 Interchange Format Files.        10/8/85 */
  5. /*                                    */
  6. /* By Jerry Morrison and Steve Shaw, Electronic Arts.            */
  7. /* This software is in the public domain.                */
  8. /*----------------------------------------------------------------------*/
  9.  
  10. typedef LONG IFFP;    /* Status code result from an IFF procedure */
  11.     /* LONG, because must be type compatable with ID for GetChunkHdr.*/
  12.     /* Note that the error codes below are not legal IDs.*/
  13. #define IFF_OKAY  0    /* Keep going...*/
  14. #define END_MARK  -1    /* As if there was a chunk at end of group.*/
  15. #define IFF_DONE  -2    /* clientProc returns this when it has READ enough.
  16.              * It means return thru all levels. File is Okay.*/
  17. #define DOS_ERROR -3
  18. #define NOT_IFF   -4    /* not an IFF file.*/
  19. #define NO_FILE   -5    /* Tried to open file, DOS didn't find it.*/
  20. #define CLIENT_ERROR -6    /* Client made invalid request, for instance, asking
  21.              * for more bytes than existed in chunk.*/
  22. #define BAD_FORM  -7    /* A client read proc complains about FORM semantics;
  23.              * e.g. valid IFF, but missing a required chunk.*/
  24. #define SHORT_CHUNK -8    /* Client asked to IFFReadBytes more bytes than left
  25.              * in the chunk. Could be client bug or bad form.*/
  26. #define BAD_IFF   -9    /* mal-formed IFF file. [TBD] Expand this into a
  27.              * range of error codes.*/
  28. #define LAST_ERROR BAD_IFF
  29.  
  30. /* This MACRO is used to RETURN immediately when a termination condition is
  31.  * found. This is a pretty weird macro. It requires the caller to declare a
  32.  * local "IFFP iffp" and assign it. This wouldn't work as a subroutine since
  33.  * it returns for it's caller. */
  34. #define CheckIFFP()   { if (iffp != IFF_OKAY) return(iffp); }
  35.  
  36.  
  37. /* ---------- ID -------------------------------------------------------*/
  38.  
  39. typedef LONG ID;    /* An ID is four printable ASCII chars but
  40.              * stored as a LONG for efficient copy & compare.*/
  41.  
  42. /* Four-character IDentifier builder.*/
  43. #define MakeID(a,b,c,d)  ( (a)<<24 | (b)<<16 | (c)<<8 | (d) )
  44.  
  45. /* Standard group IDs.  A chunk with one of these IDs contains a
  46.    SubTypeID followed by zero or more chunks.*/
  47. #define FORM MakeID('F','O','R','M')
  48. #define PROP MakeID('P','R','O','P')
  49. #define LIST MakeID('L','I','S','T')
  50. #define CAT  MakeID('C','A','T',' ')
  51. #define FILLER MakeID(' ',' ',' ',' ')
  52. /* The IDs "FOR1".."FOR9", "LIS1".."LIS9", & "CAT1".."CAT9" are reserved
  53.  * for future standardization.*/
  54.  
  55. /* Pseudo-ID used internally by chunk reader and writer.*/
  56. #define NULL_CHUNK 0L           /* No current chunk.*/
  57.  
  58.  
  59. /* ---------- Chunk ----------------------------------------------------*/
  60.  
  61. /* All chunks start with a type ID and a count of the data bytes that 
  62.    follow--the chunk's "logical size" or "data size". If that number is odd,
  63.    a 0 pad byte is written, too. */
  64. typedef struct {
  65.     ID      ckID;
  66.     LONG  ckSize;
  67.     } ChunkHeader;
  68.  
  69. typedef struct {
  70.     ID      ckID;
  71.     LONG  ckSize;
  72.     UBYTE ckData[ 1 /*REALLY: ckSize*/ ];
  73.     } Chunk;
  74.  
  75. /* Pass ckSize = szNotYetKnown to the writer to mean "compute the size".*/
  76. #define szNotYetKnown 0x80000001L
  77.  
  78. /* Need to know whether a value is odd so can word-align.*/
  79. #define IS_ODD(a)   ((a) & 1)
  80.  
  81. /* This macro rounds up to an even number. */
  82. #define WordAlign(size)   ((size+1)&~1)
  83.  
  84. /* ALL CHUNKS MUST BE PADDED TO EVEN NUMBER OF BYTES.
  85.  * ChunkPSize computes the total "physical size" of a padded chunk from
  86.  * its "data size" or "logical size". */
  87. #define ChunkPSize(dataSize)  (WordAlign(dataSize) + sizeof(ChunkHeader))
  88.  
  89. /* The Grouping chunks (LIST, FORM, PROP, & CAT) contain concatenations of
  90.  * chunks after a subtype ID that identifies the content chunks.
  91.  * "FORM type XXXX", "LIST of FORM type XXXX", "PROPerties associated
  92.  * with FORM type XXXX", or "conCATenation of XXXX".*/
  93. typedef struct {
  94.     ID      ckID;
  95.     LONG  ckSize;    /* this ckSize includes "grpSubID".*/
  96.     ID    grpSubID;
  97.     } GroupHeader;
  98.  
  99. typedef struct {
  100.     ID      ckID;
  101.     LONG  ckSize;
  102.     ID    grpSubID;
  103.     UBYTE grpData[ 1 /*REALLY: ckSize-sizeof(grpSubID)*/ ];
  104.     } GroupChunk;
  105.  
  106.  
  107. /* ---------- IFF Reader -----------------------------------------------*/
  108.  
  109. /******** Routines to support a stream-oriented IFF file reader *******
  110.  *
  111.  * These routines handle lots of details like error checking and skipping
  112.  * over padding. They're also careful not to read past any containing context.
  113.  *
  114.  * These routines ASSUME they're the only ones reading from the file.
  115.  * Client should check IFFP error codes. Don't press on after an error!
  116.  * These routines try to have no side effects in the error case, except
  117.  * partial I/O is sometimes unavoidable.
  118.  *
  119.  * All of these routines may return DOS_ERROR. In that case, ask DOS for the
  120.  * specific error code.
  121.  *
  122.  * The overall scheme for the low level chunk reader is to open a "group read
  123.  * context" with OpenRIFF or OpenRGroup, read the chunks with GetChunkHdr
  124.  * (and its kin) and IFFReadBytes, and close the context with CloseRGroup.
  125.  *
  126.  * The overall scheme for reading an IFF file is to use ReadIFF, ReadIList,
  127.  * and ReadICat to scan the file. See those procedures, ClientProc (below),
  128.  * and the skeleton IFF reader. */
  129.  
  130. /* Client passes ptrs to procedures of this type to ReadIFF which call them
  131.  * back to handle LISTs, FORMs, CATs, and PROPs.
  132.  *
  133.  * Use the GroupContext ptr when calling reader routines like GetChunkHdr.
  134.  * Look inside the GroupContext ptr for your ClientFrame ptr. You'll
  135.  * want to type cast it into a ptr to your containing struct to get your
  136.  * private contextual data (stacked property settings). See below. */
  137. typedef IFFP ClientProc(/* struct _GroupContext * */);
  138.  
  139. /* Client's context for reading an IFF file or a group.
  140.  * Client should actually make this the first component of a larger struct
  141.  * (it's personal stack "frame") that has a field to store each "interesting"
  142.  * property encountered.
  143.  * Either initialize each such field to a global default or keep a boolean
  144.  * indicating if you've read a property chunk into that field.
  145.  * Your getList and getForm procs should allocate a new "frame" and copy the
  146.  * parent frame's contents. The getProp procedure should store into the frame
  147.  * allocated by getList for the containing LIST. */
  148. typedef struct _ClientFrame {
  149.     ClientProc *getList, *getProp, *getForm, *getCat;
  150.     /* client's own data follows; place to stack property settings */
  151.     } ClientFrame;
  152.  
  153. /* Our context for reading a group chunk. */
  154. typedef struct _GroupContext {
  155.     struct _GroupContext *parent; /* Containing group; NULL => whole file. */
  156.     ClientFrame *clientFrame;     /* Reader data & client's context state. */
  157.     BPTR file;        /* Byte-stream file handle. */
  158.     LONG position;    /* The context's logical file position. */
  159.     LONG bound;        /* File-absolute context bound
  160.              * or szNotYetKnown (writer only). */
  161.     ChunkHeader ckHdr;    /* Current chunk header. ckHdr.ckSize = szNotYetKnown
  162.              * means we need to go back and set the size (writer only).
  163.              * See also Pseudo-IDs, above. */
  164.     ID subtype;        /* Group's subtype ID when reading. */
  165.     LONG bytesSoFar;    /* # bytes read/written of current chunk's data. */
  166.     } GroupContext;
  167.  
  168. /* Computes the number of bytes not yet read from the current chunk, given
  169.  * a group read context gc. */
  170. #define ChunkMoreBytes(gc)  ((gc)->ckHdr.ckSize - (gc)->bytesSoFar)
  171.  
  172.  
  173. /***** Low Level IFF Chunk Reader *****/
  174.  
  175. /* Given an open file, open a read context spanning the whole file.
  176.  * This is normally only called by ReadIFF.
  177.  * This sets new->clientFrame = clientFrame.
  178.  * ASSUME context allocated by caller but not initialized.
  179.  * ASSUME caller doesn't deallocate the context before calling CloseRGroup.
  180.  * NOT_IFF ERROR if the file is too short for even a chunk header.*/
  181. extern IFFP OpenRIFF(/* BPTR, GroupContext *, ClientFrame * */);
  182.                  /* file, new,            clientFrame  */
  183.  
  184. /* Open the remainder of the current chunk as a group read context.
  185.  * This will be called just after the group's subtype ID has been read
  186.  * (automatically by GetChunkHdr for LIST, FORM, PROP, and CAT) so the
  187.  * remainder is a sequence of chunks.
  188.  * This sets new->clientFrame = parent->clientFrame. The caller should repoint
  189.  * it at a new clientFrame if opening a LIST context so it'll have a "stack
  190.  * frame" to store PROPs for the LIST. (It's usually convenient to also
  191.  * allocate a new Frame when you encounter FORM of the right type.)
  192.  *
  193.  * ASSUME new context allocated by caller but not initialized.
  194.  * ASSUME caller doesn't deallocate the context or access the parent context
  195.  * before calling CloseRGroup.
  196.  * BAD_IFF ERROR if context end is odd or extends past parent. */
  197. extern IFFP OpenRGroup(/* GroupContext *, GroupContext * */);
  198.                /* parent,         new  */
  199.  
  200. /* Close a group read context, updating its parent context.
  201.  * After calling this, the old context may be deallocated and the parent
  202.  * context can be accessed again. It's okay to call this particular procedure
  203.  * after an error has occurred reading the group.
  204.  * This always returns IFF_OKAY. */
  205. extern IFFP CloseRGroup(/* GroupContext * */);
  206.                 /* old  */
  207.  
  208. /* Skip any remaining bytes of the previous chunk and any padding, then
  209.  * read the next chunk header into context.ckHdr.
  210.  * If the ckID is LIST, FORM, CAT, or PROP, this automatically reads the
  211.  * subtype ID into context->subtype.
  212.  * Caller should dispatch on ckID (and subtype) to an appropriate handler.
  213.  *
  214.  * RETURNS context.ckHdr.ckID (the ID of the new chunk header); END_MARK
  215.  * if there are no more chunks in this context; or NOT_IFF if the top level
  216.  * file chunk isn't a FORM, LIST, or CAT; or BAD_IFF if malformed chunk, e.g.
  217.  * ckSize is negative or too big for containing context, ckID isn't positive,
  218.  * or we hit end-of-file.
  219.  *
  220.  * See also GetFChunkHdr, GetF1ChunkHdr, and GetPChunkHdr, below.*/
  221. extern ID       GetChunkHdr(/* GroupContext * */);
  222.   /*  context.ckHdr.ckID       context  */
  223.  
  224. /* Read nBytes number of data bytes of current chunk. (Use OpenGroup, etc.
  225.  * instead to read the contents of a group chunk.) You can call this several
  226.  * times to read the data piecemeal.
  227.  * CLIENT_ERROR if nBytes < 0. SHORT_CHUNK if nBytes > ChunkMoreBytes(context)
  228.  * which could be due to a client bug or a chunk that's shorter than it
  229.  * ought to be (bad form). (on either CLIENT_ERROR or SHORT_CHUNK,
  230.  * IFFReadBytes won't read any bytes.) */
  231. extern IFFP IFFReadBytes(/* GroupContext *, BYTE *, LONG */);
  232.                  /* context,        buffer, nBytes  */
  233.  
  234.  
  235. /***** IFF File Reader *****/
  236.  
  237. /* This is a noop ClientProc that you can use for a getList, getForm, getProp,
  238.  * or getCat procedure that just skips the group. A simple reader might just
  239.  * implement getForm, store &ReadICat in the getCat field of clientFrame, and
  240.  * use &SkipGroup for the getList and getProp procs.*/
  241. extern IFFP SkipGroup(/* GroupContext * */);
  242.  
  243. /* IFF file reader.
  244.  * Given an open file, allocate a group context and use it to read the FORM,
  245.  * LIST, or CAT and it's contents. The idea is to parse the file's contents,
  246.  * and for each FORM, LIST, CAT, or PROP encountered, call the getForm,
  247.  * getList, getCat, or getProp procedure in clientFrame, passing the
  248.  * GroupContext ptr.
  249.  * This is achieved with the aid of ReadIList (which your getList should
  250.  * call) and ReadICat (which your getCat should call, if you don't just use
  251.  * &ReadICat for your getCat). If you want to handle FORMs, LISTs, and CATs
  252.  * nested within FORMs, the getForm procedure must dispatch to getForm,
  253.  * getList, and getCat (it can use GetF1ChunkHdr to make this easy).
  254.  *
  255.  * Normal return is IFF_OKAY (if whole file scanned) or IFF_DONE (if a client
  256.  * proc said "done" first).
  257.  * See the skeletal getList, getForm, getCat, and getProp procedures. */
  258. extern IFFP ReadIFF(/* BPTR, ClientFrame * */);
  259.                     /* file, clientFrame  */
  260.  
  261. /* IFF LIST reader.
  262.  * Your "getList" procedure should allocate a ClientFrame, copy the parent's
  263.  * ClientFrame, and then call this procedure to do all the work.
  264.  *
  265.  * Normal return is IFF_OKAY (if whole LIST scanned) or IFF_DONE (if a client
  266.  * proc said "done" first).
  267.  * BAD_IFF ERROR if a PROP appears after a non-PROP. */
  268. extern IFFP ReadIList(/* GroupContext *, ClientFrame * */);
  269.               /* parent,         clientFrame  */
  270.  
  271. /* IFF CAT reader.
  272.  * Most clients can simply use this to read their CATs. If you must do extra
  273.  * setup work, put a ptr to your getCat procedure in the clientFrame, and
  274.  * have that procedure call ReadICat to do the detail work.
  275.  *
  276.  * Normal return is IFF_OKAY (if whole CAT scanned) or IFF_DONE (if a client
  277.  * proc said "done" first).
  278.  * BAD_IFF ERROR if a PROP appears in the CAT. */
  279. extern IFFP ReadICat(/* GroupContext * */);
  280.              /* parent  */
  281.  
  282. /* Call GetFChunkHdr instead of GetChunkHdr to read each chunk inside a FORM.
  283.  * It just calls GetChunkHdr and returns BAD_IFF if it gets a PROP chunk. */
  284. extern ID    GetFChunkHdr(/* GroupContext * */);
  285.   /*  context.ckHdr.ckID        context  */
  286.  
  287. /* GetF1ChunkHdr is like GetFChunkHdr, but it automatically dispatches to the
  288.  * getForm, getList, and getCat procedure (and returns the result) if it
  289.  * encounters a FORM, LIST, or CAT. */
  290. extern ID    GetF1ChunkHdr(/* GroupContext * */);
  291.   /*  context.ckHdr.ckID         context  */
  292.  
  293. /* Call GetPChunkHdr instead of GetChunkHdr to read each chunk inside a PROP.
  294.  * It just calls GetChunkHdr and returns BAD_IFF if it gets a group chunk. */
  295. extern ID    GetPChunkHdr(/* GroupContext * */);
  296.   /*  context.ckHdr.ckID        context  */
  297.  
  298.  
  299. /* ---------- IFF Writer -----------------------------------------------*/
  300.  
  301. /******* Routines to support a stream-oriented IFF file writer *******
  302.  *
  303.  * These routines will random access back to set a chunk size value when the
  304.  * caller doesn't know it ahead of time. They'll also do things automatically
  305.  * like padding and error checking.
  306.  *
  307.  * These routines ASSUME they're the only ones writing to the file.
  308.  * Client should check IFFP error codes. Don't press on after an error!
  309.  * These routines try to have no side effects in the error case, except that
  310.  * partial I/O is sometimes unavoidable.
  311.  *
  312.  * All of these routines may return DOS_ERROR. In that case, ask DOS for the
  313.  * specific error code.
  314.  *
  315.  * The overall scheme is to open an output GroupContext via OpenWIFF or
  316.  * OpenWGroup, call either PutCk or {PutCkHdr {IFFWriteBytes}* PutCkEnd} for
  317.  * each chunk, then use CloseWGroup to close the GroupContext.
  318.  *
  319.  * To write a group (LIST, FORM, PROP, or CAT), call StartWGroup, write out
  320.  * its chunks, then call EndWGroup. StartWGroup automatically writes the
  321.  * group header and opens a nested context for writing the contents.
  322.  * EndWGroup closes the nested context and completes the group chunk. */
  323.  
  324.  
  325. /* Given a file open for output, open a write context.
  326.  * The "limit" arg imposes a fence or upper limit on the logical file
  327.  * position for writing data in this context. Pass in szNotYetKnown to be
  328.  * bounded only by disk capacity.
  329.  * ASSUME new context structure allocated by caller but not initialized.
  330.  * ASSUME caller doesn't deallocate the context before calling CloseWGroup.
  331.  * The caller is only allowed to write out one FORM, LIST, or CAT in this top
  332.  * level context (see StartWGroup and PutCkHdr).
  333.  * CLIENT_ERROR if limit is odd.*/
  334. extern IFFP OpenWIFF(/* BPTR, GroupContext *, LONG */);
  335.              /* file, new,            limit {file position}  */
  336.  
  337. /* Start writing a group (presumably LIST, FORM, PROP, or CAT), opening a
  338.  * nested context. The groupSize includes all nested chunks + the subtype ID.
  339.  *
  340.  * The subtype of a LIST or CAT is a hint at the contents' FORM type(s). Pass
  341.  * in FILLER if it's a mixture of different kinds.
  342.  *
  343.  * This writes the chunk header via PutCkHdr, writes the subtype ID via
  344.  * IFFWriteBytes, and calls OpenWGroup. The caller may then write the nested
  345.  * chunks and finish by calling EndWGroup.
  346.  * The OpenWGroup call sets new->clientFrame = parent->clientFrame.
  347.  *
  348.  * ASSUME new context structure allocated by caller but not initialized.
  349.  * ASSUME caller doesn't deallocate the context or access the parent context
  350.  * before calling CloseWGroup.
  351.  * ERROR conditions: See PutCkHdr, IFFWriteBytes, OpenWGroup. */
  352. extern IFFP StartWGroup(/* GroupContext *, ID, LONG, ID, GroupContext * */);
  353.                 /* parent, groupType, groupSize, subtype, new  */
  354.  
  355. /* End a group started by StartWGroup.
  356.  * This just calls CloseWGroup and PutCkEnd.
  357.  * ERROR conditions: See CloseWGroup and PutCkEnd. */
  358. extern IFFP EndWGroup(/* GroupContext * */);
  359.               /* old  */
  360.  
  361. /* Open the remainder of the current chunk as a group write context.
  362.  * This is normally only called by StartWGroup.
  363.  *
  364.  * Any fixed limit to this group chunk or a containing context will impose
  365.  * a limit on the new context.
  366.  * This will be called just after the group's subtype ID has been written
  367.  * so the remaining contents will be a sequence of chunks.
  368.  * This sets new->clientFrame = parent->clientFrame.
  369.  * ASSUME new context structure allocated by caller but not initialized.
  370.  * ASSUME caller doesn't deallocate the context or access the parent context
  371.  * before calling CloseWGroup.
  372.  * CLIENT_ERROR if context end is odd or PutCkHdr wasn't called first. */
  373. extern IFFP OpenWGroup(/* GroupContext *, GroupContext * */);
  374.                /* parent,         new  */
  375.  
  376. /* Close a write context and update its parent context.
  377.  * This is normally only called by EndWGroup.
  378.  *
  379.  * If this is a top level context (created by OpenWIFF) we'll set the file's
  380.  * EOF (end of file) but won't close the file.
  381.  * After calling this, the old context may be deallocated and the parent
  382.  * context can be accessed again.
  383.  *
  384.  * Amiga DOS Note: There's no call to set the EOF. We just position to the
  385.  * desired end and return. Caller must Close file at that position.
  386.  * CLIENT_ERROR if PutCkEnd wasn't called first. */
  387. extern IFFP CloseWGroup(/* GroupContext * */);
  388.                 /* old  */
  389.  
  390. /* Write a whole chunk to a GroupContext. This writes a chunk header, ckSize
  391.  * data bytes, and (if needed) a pad byte. It also updates the GroupContext.
  392.  * CLIENT_ERROR if ckSize == szNotYetKnown. See also PutCkHdr errors. */
  393. extern IFFP PutCk(/* GroupContext *, ID,   LONG,   BYTE * */);
  394.               /* context,        ckID, ckSize, *data  */
  395.  
  396. /* Write just a chunk header. Follow this will any number of calls to
  397.  * IFFWriteBytes and finish with PutCkEnd.
  398.  * If you don't yet know how big the chunk is, pass in ckSize = szNotYetKnown,
  399.  * then PutCkEnd will set the ckSize for you later.
  400.  * Otherwise, IFFWriteBytes and PutCkEnd will ensure that the specified
  401.  * number of bytes get written.
  402.  * CLIENT_ERROR if the chunk would overflow the GroupContext's bound, if
  403.  * PutCkHdr was previously called without a matching PutCkEnd, if ckSize < 0
  404.  * (except szNotYetKnown), if you're trying to write something other
  405.  * than one FORM, LIST, or CAT in a top level (file level) context, or
  406.  * if ckID <= 0 (these illegal ID values are used for error codes). */
  407. extern IFFP PutCkHdr(/* GroupContext *, ID,   LONG */);
  408.              /* context,        ckID, ckSize  */
  409.  
  410. /* Write nBytes number of data bytes for the current chunk and update
  411.  * GroupContext.
  412.  * CLIENT_ERROR if this would overflow the GroupContext's limit or the
  413.  * current chunk's ckSize, or if PutCkHdr wasn't called first, or if
  414.  * nBytes < 0. */
  415. extern IFFP IFFWriteBytes(/* GroupContext *, BYTE *, LONG */);
  416.                   /* context,        *data,  nBytes  */
  417.  
  418. /* Complete the current chunk, write a pad byte if needed, and update
  419.  * GroupContext.
  420.  * If current chunk's ckSize = szNotYetKnown, this goes back and sets the
  421.  * ckSize in the file.
  422.  * CLIENT_ERROR if PutCkHdr wasn't called first, or if client hasn't
  423.  * written 'ckSize' number of bytes with IFFWriteBytes. */
  424. extern IFFP PutCkEnd(/* GroupContext * */);
  425.              /* context  */
  426.  
  427. #endif
  428.  
  429.